package com.yadong.smarthome2.datastore;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SmartHomeDbHelper extends SQLiteOpenHelper {
    private static final int CURRENT_VERSION = 1;
    private static final String DATABASE_NAME = "smart_home";

    public SmartHomeDbHelper(Context context) {
        super(context, DATABASE_NAME, null, CURRENT_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + Tables.Device.TABLE_NAME + " ("
                + Tables.Device.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + Tables.Device.CONNECTION_TYPE + " INTEGER,"
                + Tables.Device.TYPE + " INTEGER,"
                + Tables.Device.NAME + " TEXT,"
                + Tables.Device.DESCRIPTION + " TEXT,"
                + Tables.Device.ADDRESS + " TEXT,"
                + Tables.Device.MAC + " TEXT,"
                + Tables.Device.DEVICE_INFO_VERSION + " INTEGER)");

        db.execSQL("CREATE TABLE " + Tables.Function.TABLE_NAME + " ("
                + Tables.Function.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + Tables.Function.NAME + " TEXT,"
                + Tables.Function.CODE + " TEXT,"
                + Tables.Function.DEVICE_ID + " INTEGER REFERENCES "
                + Tables.Device.TABLE_NAME + "(" + Tables.Device.ID + "))");

        db.execSQL("CREATE TABLE " + Tables.Argument.TABLE_NAME + " ("
                + Tables.Argument.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + Tables.Argument.NAME + " TEXT,"
                + Tables.Argument.TYPE + " TEXT,"
                + Tables.Argument.LIMIT + " TEXT,"
                + Tables.Argument.FUNCTION_ID + " INTEGER REFERENCES "
                + Tables.Function.TABLE_NAME + "(" + Tables.Function.ID + "))");

        db.execSQL("CREATE TABLE " + Tables.Status.TABLE_NAME + " ("
                + Tables.Status.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + Tables.Status.NAME + " TEXT,"
                + Tables.Status.TYPE + " TEXT,"
                + Tables.Status.DEVICE_ID + " INTEGER REFERENCES "
                + Tables.Device.TABLE_NAME + "(" + Tables.Device.ID + "))");

        db.execSQL("CREATE TABLE " + Tables.Rule.TABLE_NAME + " ("
               + Tables.Rule.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
               + Tables.Rule.STATUSES + " TEXT,"
               + Tables.Rule.MIN + " TEXT,"
               + Tables.Rule.MAX + " TEXT,"
               + Tables.Rule.JUST_RIGHT + " TEXT,"
               + Tables.Rule.FUNCTION_CODE + " TEXT,"
               + Tables.Rule.PARAMETERS + " TEXT,"
               + Tables.Rule.DEVICE_ID + " INTEGER,"
               + Tables.Rule.TARGET_DEVICE_ID + " INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

}
